@using BlazorApp.Utils.Prometheus.Models.Interfaces
@inherits BlazorApp.Pages.Common.PageBase

@if (AllMetrics is { Count: > 0 })
{
    @if (AllMetrics.Any(x => x.Name == MetricName))
    {
        <GridRow Class="grid-table">
            <GridCol Span="16">@Title</GridCol>
            <GridCol Span="8">
                @{
                    var count = AllMetrics
                        .FirstOrDefault(x => x.Name == MetricName)?
                        .Measurements
                        .Where(x => x.Name == $"{MetricName}_count")
                        .Sum(x => x.Value);
                    var seconds = AllMetrics
                        .FirstOrDefault(x => x.Name == MetricName)?
                        .Measurements
                        .Where(x => x.Name == $"{MetricName}_sum")
                        .Sum(x => x.Value);
                    var avg = seconds / count;
                }
                <NumberFormat Number="@avg" Unit="@Unit"></NumberFormat>
                <Button Type="@ButtonType.Link" OnClick=@(() => _showDetail = true)>@L["Detail"]</Button>
            </GridCol>
        </GridRow>

        <Modal Closable="true" @bind-Visible="@_showDetail"
               Width="1000" Resizable=true
               Style="top: 20px"
               Title=@Title>

            @if (MultipleGroupBy is { Count: > 0 })
            {
                <Tabs Type="@TabType.Card">
                    <TabPane Key="1" Tab="@L["PercentileChart"]">
                        <MetricsHistogramPercentGroupByView AllMetrics="@AllMetrics"
                                                            MetricName="@MetricName"
                                                            Title=@L[MetricName]
                                                            MultipleGroupBy="@MultipleGroupBy"
                                                            Unit="@Unit">
                        </MetricsHistogramPercentGroupByView>
                    </TabPane>
                    <TabPane Key="2" Tab="@L["DataList"]">
                        <MetircsHistogramGroupByMultipleAvgView AllMetrics="@AllMetrics"
                                                                MetricName="@MetricName"
                                                                Unit="@Unit"
                                                                MultipleGroupBy="@MultipleGroupBy">
                        </MetircsHistogramGroupByMultipleAvgView>
                    </TabPane>

                </Tabs>
            }
            else
            {
                <MetricsHistogramPercentView AllMetrics="@AllMetrics"
                                             MetricName="@MetricName"
                                             Title=@L[MetricName]
                                             Unit="@Unit">
                </MetricsHistogramPercentView>
            }
        </Modal>
    }
}

@code {

    [Parameter] public IList<IMetric> AllMetrics { get; set; }
    [Parameter] public string MetricName { get; set; }
    [Parameter] public string Title { get; set; }
    [Parameter] public string Unit { get; set; }
    [Parameter] public List<string> MultipleGroupBy { get; set; }
    private bool _showDetail = false;


}
